home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / Glypha III Src Folder / Code ƒ / Interface.c < prev    next >
Encoding:
Text File  |  1995-01-30  |  10.7 KB  |  539 lines  |  [TEXT/MPCC]

  1.  
  2. //============================================================================
  3. //----------------------------------------------------------------------------
  4. //                                    Interface.c
  5. //----------------------------------------------------------------------------
  6. //============================================================================
  7.  
  8.  
  9. #include "Externs.h"
  10. #include <Sound.h>
  11.  
  12.  
  13. #define kAppleMenuID        128
  14. #define iAbout                1
  15. #define kGameMenuID            129
  16. #define iNewGame            1
  17. #define iPauseGame            2
  18. #define iEndGame            3
  19. #define iQuit                5
  20. #define kOptionsMenuID        130
  21. #define iSettings            1
  22. #define iHelp                2
  23. #define iHighScores            3
  24. #define kAboutPictID        132
  25.  
  26.  
  27. void DoAppleMenu (short);
  28. void DoGameMenu (short);
  29. void DoOptionsMenu (short);
  30. void UpdateMainWindow (void);
  31. void HandleMouseEvent (EventRecord *);
  32. void HandleKeyEvent (EventRecord *);
  33. void HandleUpdateEvent (EventRecord *);
  34. void HandleOSEvent (EventRecord *);
  35. void HandleHighLevelEvent (EventRecord *);
  36. void DoAbout (void);
  37. void DoGameSettings (void);
  38.  
  39.  
  40. Rect        mainWindowRect;
  41. WindowPtr    mainWindow;
  42. MenuHandle    appleMenu, gameMenu, optionsMenu;
  43. Boolean        switchedOut, quitting, canPlay, openTheScores;
  44.  
  45. extern    prefsInfo    thePrefs;
  46. extern    Rect        backSrcRect, workSrcRect;
  47. extern    CGrafPtr    backSrcMap, workSrcMap;
  48. extern    Boolean        pausing, playing, helpOpen, scoresOpen;
  49.  
  50.  
  51. //==============================================================  Functions
  52. //--------------------------------------------------------------  MenusReflectMode
  53.  
  54. void MenusReflectMode (void)
  55. {
  56.     if (playing)
  57.     {
  58.         DisableItem(gameMenu, iNewGame);
  59.         EnableItem(gameMenu, iPauseGame);
  60.         if (pausing)
  61.             SetItem(gameMenu, iPauseGame, "\pResume Game");
  62.         else
  63.             SetItem(gameMenu, iPauseGame, "\pPause Game");
  64.         EnableItem(gameMenu, iEndGame);
  65.         DisableItem(optionsMenu, 0);
  66.     }
  67.     else
  68.     {
  69.         EnableItem(gameMenu, iNewGame);
  70.         DisableItem(gameMenu, iPauseGame);
  71.         SetItem(gameMenu, iPauseGame, "\pPause Game");
  72.         DisableItem(gameMenu, iEndGame);
  73.         EnableItem(optionsMenu, 0);
  74.     }
  75. }
  76.  
  77. //--------------------------------------------------------------  DoAppleMenu
  78.  
  79. void DoAppleMenu (short theItem)
  80. {
  81.     Str255        daName;
  82.     GrafPtr        wasPort;
  83.     short        daNumber;
  84.     
  85.     switch (theItem)
  86.     {
  87.         case iAbout:
  88.         if ((scoresOpen) || (helpOpen))
  89.         {
  90.             CloseWall();
  91.             scoresOpen = FALSE;
  92.             helpOpen = FALSE;
  93.             CheckItem(optionsMenu, iHelp, helpOpen);
  94.             CheckItem(optionsMenu, iHighScores, scoresOpen);
  95.         }
  96.         DoAbout();
  97.         break;
  98.         
  99.         default:
  100.         GetItem(appleMenu, theItem, daName);
  101.         GetPort(&wasPort);
  102.         daNumber = OpenDeskAcc(daName);
  103.         SetPort((GrafPtr)wasPort);
  104.         break;
  105.     }
  106. }
  107.  
  108. //--------------------------------------------------------------  DoGameMenu
  109.  
  110. void DoGameMenu (short theItem)
  111. {
  112.     switch (theItem)
  113.     {
  114.         case iNewGame:
  115.         if ((scoresOpen) || (helpOpen))
  116.         {
  117.             CloseWall();
  118.             scoresOpen = FALSE;
  119.             helpOpen = FALSE;
  120.             CheckItem(optionsMenu, iHelp, helpOpen);
  121.             CheckItem(optionsMenu, iHighScores, scoresOpen);
  122.         }
  123.         InitNewGame();
  124.         MenusReflectMode();
  125.         break;
  126.         
  127.         case iPauseGame:
  128.         if (pausing)
  129.         {
  130.             pausing = FALSE;
  131.             DumpBackToWorkMap();
  132.         }
  133.         break;
  134.         
  135.         case iEndGame:
  136.         break;
  137.         
  138.         case iQuit:
  139.         quitting = TRUE;
  140.         break;
  141.     }
  142. }
  143.  
  144. //--------------------------------------------------------------  DoOptionsMenu
  145.  
  146. void DoOptionsMenu (short theItem)
  147. {
  148.     switch (theItem)
  149.     {
  150.         case iSettings:
  151.         if ((scoresOpen) || (helpOpen))
  152.         {
  153.             CloseWall();
  154.             scoresOpen = FALSE;
  155.             helpOpen = FALSE;
  156.             CheckItem(optionsMenu, iHelp, helpOpen);
  157.             CheckItem(optionsMenu, iHighScores, scoresOpen);
  158.         }
  159.         DoGameSettings();
  160.         break;
  161.         
  162.         case iHelp:
  163.         if (helpOpen)
  164.         {
  165.             CloseWall();
  166.             helpOpen = FALSE;
  167.         }
  168.         else
  169.         {
  170.             if (scoresOpen)
  171.             {
  172.                 CloseWall();
  173.                 scoresOpen = FALSE;
  174.                 CheckItem(optionsMenu, iHighScores, scoresOpen);
  175.             }
  176.             OpenHelp();
  177.         }
  178.         CheckItem(optionsMenu, iHelp, helpOpen);
  179.         break;
  180.         
  181.         case iHighScores:
  182.         if (scoresOpen)
  183.         {
  184.             CloseWall();
  185.             scoresOpen = FALSE;
  186.         }
  187.         else
  188.         {
  189.             if (helpOpen)
  190.             {
  191.                 CloseWall();
  192.                 helpOpen = FALSE;
  193.                 CheckItem(optionsMenu, iHelp, helpOpen);
  194.             }
  195.             OpenHighScores();
  196.         }
  197.         CheckItem(optionsMenu, iHighScores, scoresOpen);
  198.         break;
  199.     }
  200. }
  201.  
  202. //--------------------------------------------------------------  DoMenuChoice
  203.  
  204. void DoMenuChoice (long menuChoice)
  205. {
  206.     short        theMenu, theItem;
  207.     
  208.     if (menuChoice == 0)
  209.         return;
  210.     
  211.     theMenu = HiWord(menuChoice);
  212.     theItem = LoWord(menuChoice);
  213.     
  214.     switch (theMenu)
  215.     {
  216.         case kAppleMenuID:
  217.         DoAppleMenu(theItem);
  218.         break;
  219.         
  220.         case kGameMenuID:
  221.         DoGameMenu(theItem);
  222.         break;
  223.         
  224.         case kOptionsMenuID:
  225.         DoOptionsMenu(theItem);
  226.         break;
  227.     }
  228.     
  229.     HiliteMenu(0);
  230. }
  231.  
  232. //--------------------------------------------------------------  UpdateMainWindow
  233.  
  234. void UpdateMainWindow (void)
  235. {
  236.     CopyBits(&((GrafPtr)backSrcMap)->portBits, 
  237.             &(((GrafPtr)mainWindow)->portBits), 
  238.             &mainWindowRect, &mainWindowRect, 
  239.             srcCopy, 0L);
  240. }
  241.  
  242. //--------------------------------------------------------------  HandleMouseEvent
  243.  
  244. void HandleMouseEvent (EventRecord *theEvent)
  245. {
  246.     WindowPtr    whichWindow;
  247.     long        menuChoice;
  248.     short        thePart;
  249.     
  250.     thePart = FindWindow(theEvent->where, &whichWindow);
  251.     
  252.     switch (thePart)
  253.     {
  254.         case inSysWindow:
  255.         SystemClick(theEvent, whichWindow);
  256.         break;
  257.         
  258.         case inMenuBar:
  259.         menuChoice = MenuSelect(theEvent->where);
  260.         if (canPlay)
  261.             DoMenuChoice(menuChoice);
  262.         break;
  263.         
  264.         case inDrag:
  265.         case inGoAway:
  266.         case inGrow:
  267.         case inZoomIn:
  268.         case inZoomOut:
  269.         break;
  270.         
  271.         case inContent:
  272.         FlashObelisks(TRUE);
  273.         LogNextTick(3);
  274.         GenerateLightning(theEvent->where.h, theEvent->where.v - 20);
  275.         StrikeLightning();
  276.         WaitForNextTick();
  277.         StrikeLightning();
  278.         LogNextTick(2);
  279.         WaitForNextTick();
  280.         PlayExternalSound(kLightningSound, kLightningPriority);
  281.         LogNextTick(3);
  282.         GenerateLightning(theEvent->where.h, theEvent->where.v - 20);
  283.         StrikeLightning();
  284.         WaitForNextTick();
  285.         StrikeLightning();
  286.         LogNextTick(2);
  287.         WaitForNextTick();
  288.         LogNextTick(3);
  289.         GenerateLightning(theEvent->where.h, theEvent->where.v - 20);
  290.         StrikeLightning();
  291.         WaitForNextTick();
  292.         StrikeLightning();
  293.         LogNextTick(2);
  294.         WaitForNextTick();
  295.         PlayExternalSound(kLightningSound, kLightningPriority);
  296.         LogNextTick(3);
  297.         GenerateLightning(theEvent->where.h, theEvent->where.v - 20);
  298.         StrikeLightning();
  299.         WaitForNextTick();
  300.         StrikeLightning();
  301.         LogNextTick(2);
  302.         WaitForNextTick();
  303.         FlashObelisks(FALSE);
  304.         break;
  305.     }
  306. }
  307.  
  308. //--------------------------------------------------------------  HandleKeyEvent
  309.  
  310. void HandleKeyEvent (EventRecord *theEvent)
  311. {
  312.     char        theChar;
  313.     Boolean        commandDown;
  314.     
  315.     theChar = theEvent->message & charCodeMask;
  316.     commandDown = ((theEvent->modifiers & cmdKey) != 0);
  317.     
  318.     if (commandDown)
  319.     {
  320.         if (canPlay)
  321.             DoMenuChoice(MenuKey(theChar));
  322.     }
  323.     else
  324.     {
  325.         if (helpOpen)
  326.         {
  327.             if (theChar == kUpArrowKeyASCII)
  328.             {
  329.                 if (theEvent->what == autoKey)
  330.                     ScrollHelp(-3);
  331.                 else
  332.                     ScrollHelp(-1);
  333.             }
  334.             else if (theChar == kDownArrowKeyASCII)
  335.             {
  336.                 if (theEvent->what == autoKey)
  337.                     ScrollHelp(3);
  338.                 else
  339.                     ScrollHelp(1);
  340.             }
  341.             else if (theChar == kPageDownKeyASCII)
  342.             {
  343.                 ScrollHelp(199);
  344.             }
  345.             else if (theChar == kPageUpKeyASCII)
  346.             {
  347.                 ScrollHelp(-199);
  348.             }
  349.             else if ((theChar == kHelpKeyASCII) && (!playing))
  350.             {
  351.                 CloseWall();
  352.                 helpOpen = FALSE;
  353.                 CheckItem(optionsMenu, iHelp, helpOpen);
  354.             }
  355.         }
  356.         else if ((theChar == kHelpKeyASCII) && (!playing))
  357.         {
  358.             if (scoresOpen)
  359.             {
  360.                 CloseWall();
  361.                 scoresOpen = FALSE;
  362.                 CheckItem(optionsMenu, iHighScores, scoresOpen);
  363.             }
  364.             OpenHelp();
  365.             CheckItem(optionsMenu, iHelp, helpOpen);
  366.         }
  367.     }
  368. }
  369.  
  370. //--------------------------------------------------------------  HandleUpdateEvent
  371.  
  372. void HandleUpdateEvent (EventRecord *theEvent)
  373. {    
  374.     if ((WindowPtr)theEvent->message == mainWindow)
  375.     {
  376.         SetPort((GrafPtr)mainWindow);
  377.         BeginUpdate((GrafPtr)mainWindow);
  378.         UpdateMainWindow();
  379.         EndUpdate((GrafPtr)mainWindow);
  380.         canPlay = TRUE;
  381.     }
  382. }
  383.  
  384. //--------------------------------------------------------------  HandleOSEvent
  385.  
  386. void HandleOSEvent (EventRecord *theEvent)
  387. {    
  388.     if (theEvent->message & 0x01000000)        // suspend or resume event
  389.     {
  390.         if (theEvent->message & 0x00000001)    // resume event
  391.         {
  392.             switchedOut = FALSE;
  393.         }
  394.         else                                // suspend event
  395.         {
  396.             switchedOut = TRUE;
  397.         }
  398.     }
  399. }
  400.  
  401. //--------------------------------------------------------------  HandleHighLevelEvent
  402.  
  403. void HandleHighLevelEvent (EventRecord *theEvent)
  404. {    
  405. //    theErr = AEProcessAppleEvent(theEvent);
  406. }
  407.  
  408. //--------------------------------------------------------------  HandleEvent
  409.  
  410. void HandleEvent (void)
  411. {
  412.     EventRecord    theEvent;
  413.     long        sleep = 1L;
  414.     Boolean        itHappened;
  415.     
  416.     itHappened = WaitNextEvent(everyEvent, &theEvent, sleep, 0L);
  417.     
  418.     if (itHappened)
  419.     {
  420.         switch (theEvent.what)
  421.         {
  422.             case mouseDown:
  423.             HandleMouseEvent(&theEvent);
  424.             break;
  425.             
  426.             case keyDown:
  427.             case autoKey:
  428.             HandleKeyEvent(&theEvent);
  429.             break;
  430.             
  431.             case updateEvt:
  432.             HandleUpdateEvent(&theEvent);
  433.             break;
  434.             
  435.             case osEvt:
  436.             HandleOSEvent(&theEvent);
  437.             break;
  438.             
  439.             case kHighLevelEvent:
  440.             HandleHighLevelEvent(&theEvent);
  441.             break;
  442.         }
  443.     }
  444.     else if (openTheScores)
  445.     {
  446.         openTheScores = FALSE;
  447.         OpenHighScores();
  448.     }
  449. }
  450.  
  451. //--------------------------------------------------------------  DoAbout
  452.  
  453. void DoAbout (void)
  454. {
  455.     Rect        aboutRect;
  456.     WindowPtr    aboutWindow;
  457.     
  458.     SetRect(&aboutRect, 0, 0, 325, 318);
  459.     CenterRectInRect(&aboutRect, &qd.screenBits.bounds);
  460.     aboutWindow = GetNewCWindow(129, 0L, kPutInFront);
  461.     MoveWindow((GrafPtr)aboutWindow, aboutRect.left, aboutRect.top, TRUE);
  462.     ShowWindow((GrafPtr)aboutWindow);
  463.     SetPort((GrafPtr)aboutWindow);
  464.     LoadGraphic(kAboutPictID);
  465.     
  466.     do
  467.     {
  468.     }
  469.     while (Button());
  470.     do
  471.     {
  472.     }
  473.     while (!Button());
  474.     
  475.     FlushEvents(everyEvent, 0);
  476.     
  477.     if (aboutWindow != 0L)
  478.         DisposeWindow(aboutWindow);
  479. }
  480.  
  481. //--------------------------------------------------------------  DoGameSettings
  482.  
  483. void DoGameSettings (void)
  484. {
  485.     #define        kGameSettingsDialogID    133
  486.     DialogPtr    theDial;
  487.     long        newVolume;
  488.     short        i, item;
  489.     Boolean        leaving;
  490.     
  491.     CenterDialog(kGameSettingsDialogID);
  492.     theDial = GetNewDialog(kGameSettingsDialogID, 0L, kPutInFront);
  493.     SetPort((GrafPtr)theDial);
  494.     ShowWindow((GrafPtr)theDial);
  495.     DrawDefaultButton(theDial);
  496.     FlushEvents(everyEvent, 0);
  497.     
  498.     SetDialogNumToStr(theDial, 3, (long)thePrefs.wasVolume);
  499.     SelIText(theDial, 3, 0, 1024);
  500.     
  501.     leaving = FALSE;
  502.     
  503.     while (!leaving)
  504.     {
  505.         ModalDialog(0L, &item);
  506.         
  507.         if (item == 1)
  508.         {
  509.             GetDialogNumFromStr(theDial, 3, &newVolume);
  510.             if ((newVolume >= 0) && (newVolume <= 7))
  511.             {
  512.                 thePrefs.wasVolume = (short)newVolume;
  513.                 SetSoundVol((short)newVolume);
  514.                 leaving = TRUE;
  515.             }
  516.             else
  517.             {
  518.                 SysBeep(1);
  519.                 SetDialogNumToStr(theDial, 3, (long)thePrefs.wasVolume);
  520.                 SelIText(theDial, 3, 0, 1024);
  521.             }
  522.         }
  523.         else if (item == 2)
  524.         {
  525.             for (i = 0; i < 10; i++)
  526.             {
  527.                 PasStringCopy("\pNemo", thePrefs.highNames[i]);
  528.                 thePrefs.highScores[i] = 0L;
  529.                 thePrefs.highLevel[i] = 0;
  530.                 openTheScores = TRUE;
  531.             }
  532.             DisableControl(theDial, 2);
  533.         }
  534.     }
  535.     
  536.     DisposDialog(theDial);
  537. }
  538.  
  539.